package com.example.campusrear.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.campusrear.Service.*;
import com.example.campusrear.Utils.R;
import com.example.campusrear.entity.*;
import com.example.campusrear.entity.Class;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;

    @Resource
    private PositionService positionService;

    @Resource
    private ClassService classService;

    @Resource
    private ClassGradeService gradeService;

    @Resource
    private ActivityRecordService activityRecordService;

    @Resource
    private AbsentStudentService absentStudentService;

    @Resource
    private PhotoCollectionService photoCollectionService;

    @Resource
    private StudentCourseService studentCourseService;

    @GetMapping("/page")
    public R<Page<StudentDto>> page(int page, int pageSize, Long cId,String name,Long pId,Long sId){
        Page<Student> studentPage = new Page<>(page ,pageSize);
        Page<StudentDto> studentDtoPage = new Page<>();
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Student::getStudentName,name);
        if (cId!=null){
            wrapper.eq(Student::getClassId,cId);
        }
        if (pId!=null){
            wrapper.eq(Student::getPositonId,pId);
        }
        if (sId!=null){
            wrapper.eq(Student::getStudentId,sId);
        }
        studentService.page(studentPage,wrapper);
        BeanUtils.copyProperties(studentPage,studentDtoPage,"records");
        List<Student> records = studentPage.getRecords();
        List<StudentDto> studentDtos = records.stream().map(item->{
            StudentDto studentDto = new StudentDto();
            BeanUtils.copyProperties(item,studentDto);
            Long positonId = studentDto.getPositonId();
            Position position = positionService.getById(positonId);
            Class aClass = classService.getById(studentDto.getClassId());
            studentDto.setPName(position.getPositionName());
            studentDto.setCName(aClass.getClassName());
            return studentDto;
        }).collect(Collectors.toList());
        studentDtoPage.setRecords(studentDtos);
        return R.success(studentDtoPage);
    }

    @GetMapping("/allStudent")
    public R<List<Student>> allStudent(){
        List<Student> list = studentService.list();
        return R.success(list);
    }

    @PostMapping("/save")
    public R<String> save(@RequestBody Student student){
        student.setPassword(student.getIdCardNo().substring(12));
        studentService.save(student);
        return R.success("添加成功");
    }
    @PostMapping("/upload")
    public R<String> upload(@RequestBody Student student){
        studentService.updateById(student);
        return R.success("修改成功");
    }
//    @DeleteMapping("/delete/{sId}")
//    public R<String> delete(@PathVariable Long sId){
//        studentService.removeById(sId);
//        return R.success("删除成功");
//    }

    @PostMapping("/login")
    public R<Student> login(@RequestBody Student student){
        System.out.println(student.getStudentId());
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getStudentId,student.getStudentId());
        Student serviceOne = studentService.getOne(queryWrapper);
        if (serviceOne!=null){
            if (serviceOne.getPassword() .equals(student.getPassword()) ){
                return R.success(serviceOne);
            }
            return R.error("密码错误");
        }
        return R.error("账号不存在");
    }

    @DeleteMapping("/delete/{sId}")
    public R<String> delStudent(@PathVariable Long sId){
        studentService.removeById(sId);
        LambdaQueryWrapper<ActivityRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityRecord::getStudentId,sId);
        activityRecordService.remove(wrapper);
        LambdaQueryWrapper<AbsentStudent> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(AbsentStudent::getStudentId,sId);
        absentStudentService.remove(queryWrapper1);
        LambdaQueryWrapper<PhotoCollection> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(PhotoCollection::getStudentId,sId);
        photoCollectionService.remove(queryWrapper2);
        return R.success("删除成功");
    }

    @GetMapping("/getPosition")
    public R<List<Position>> getPosition(){
        List<Position> list = positionService.list();
        return R.success(list);
    }
}
